<?php
namespace app\index\controller;
session_start();
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class  Center extends Controller
{
	//个人中心主页
	public function index()
    {

	if(empty($_SESSION['home']['id'])){

		$this->error('请先登录','index/Login/login');
    }else{
        
    $list = db('l_order')
          ->alias('o')
          ->join('l_film f','o.film_name=f.filmname')
          ->where('user_id',$_SESSION['home']['id'])
          ->field('o.price,o.retime,o.status,f.filmimg')
          ->order('retime')
          ->select();
     // var_dump($list);die; 
    $this->assign('list',$list);

    return  view('Center/index');
     }

    }
    
    //跳转到个人资料修改页面
   public function infoedit()
   {
       return  view('Center/infoedit');
   }
   



   //执行个人资料修改操作
   public function infodoedit()
   {
      $list = Request()->post();
      // var_dump($list);die;
      $data = db('l_user')->where('id',$_SESSION['home']['id'])->update($list);
     // var_dump($_SESSION['home']['id']);die;
      if($data){ 
        $this->success('编辑成功','index/Center/index');
     }else{ $this->error('编辑失败');}
   }
    
    //跳转到头像修改页
    public function icon()
    {   
     $icon = db('l_user')->where('id',$_SESSION['home']['id'])->field('heard')->find();
      $this->assign('icon', $icon);

      // var_dump($icon);die;
        return view('Center/icon');
    }
    

    //保存个人头像
    public function doicon(Request $request)
    {   
       //获取老头像
        $icon = db('l_user')->where('id',$_SESSION['home']['id'])->field('heard')->find();
        // var_dump($icon);die;
       //获取表单上传的图片
        $file = Request()->file('heard');
        // var_dump($file);die;
        //移动到框架应用根目录/public/static/image 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'static/icon');
        
        
        if($info) {
            //获取图片路径
            $img = implode('/',explode('\\',$info->getSaveName()));
            $data = [
            'heard'=>$img
           ]   ;
       
       
       $id =  db('l_user')->where('id',$_SESSION['home']['id'])->update($data);
      //插入数据 成功跳转
         
         unlink('./static/icon/'.$icon['heard']);
         $this->success('添加成功','index/Center/icon');
        }else{

            //上传失败
            $this->error('图片上传失败');
        }
    


        
    }

   //前台跳转密码修改页
       public function passedit()

       {
        return view('Center/passedit');
       }

   
   //前台执行密码修改页
       public function passdoedit(Request $request)

       {
       $post = request()->post();
       // var_dump($data);die;
       $self = db('l_user')->where('id',$_SESSION['home']['id'])->find();
        if (empty($post['repwd'])|| empty($post['npwd'])||empty($post['renpwd'])){
          return $this->error('密码不能为空');
        }
        if (md5($post['repwd']) != $self['password']) {
        return $this->error('原密码不对哦!');
      }
        if($post['npwd'] != $post['renpwd']){
        return $this->error('新密码两次输入不一样'); 
        }

       $data = md5($post['npwd']);
       // var_dump($data);die;
       // var_dump($_SESSION['home']['id']);die;
       $list = db('l_user')->where('id',$_SESSION['home']['id'])->update(['password'=>$data]);


        if ($list>0){
          return $this->success('编辑成功',url('/index/Center/index'));
        } else{

           return $this->error('编辑失败');
        }
       }


      //跳转订单列表
      public function myorder()
      {
        $list = db('l_order')
          ->alias('o')
          ->join('l_film f','o.film_name=f.filmname')
          ->where('user_id',$_SESSION['home']['id'])
          ->field('o.price,o.retime,o.status,f.filmimg,o.time,o.order,o.cinema_name,o.studio_name,o.seat,o.film_name')
          ->order('retime')
          ->select();
        
          $arr = [];
          foreach ($list as  $v) {
            $seat = str_replace ('_','座',$v['seat']);
            $se = $seat.'位' ;
            $v['seat']= $se;
            $arr[] = $v; 
          }
         // var_dump($arr);die;
          $this->assign('date',$arr);
          return view('Center/myorder');
       
      }

}